<?php
/**
 * | @author 韩威兵^life <1877361302@qq.com>
 * +----------------------------------------------------------------------
 * | @copyright 2016 - 2019 HQ
 * +----------------------------------------------------------------------
 * | @version $Id: 2019/4/25 15:23 jiahe_new index.php 韩威兵^life $
 * +----------------------------------------------------------------------
 */

namespace app\api\controller;

use app\api\server\order\OrderServer;
use app\common\common\Server\dingSendErrorMsgServer;
use app\common\common\Server\TemplateMessageServer;
use app\api\server\food\FoodServer;
use app\api\server\member\memberServer;
use app\common\common\model\member\member;
use app\common\common\model\oldModel\goods\Goods;
use app\common\common\Server\commonServer;
use EasyWeChat\MiniProgram\Sns\Sns;
use think\Loader;

class Index extends Base
{


    /**
     * 用来测试的接口
     */
    public function index()
    {
       
        commonServer::success([], '欢迎来到嘉和空间')->exits();
    }


    /**
     * 查看网站是否是维护时间
     */
    public function webStatus()
    {
        commonServer::success([], '正常营业')->exits();
    }


    /**
     * 用户的登录和注册
     * todo aw: 需要同步用户数据到参兴建去
     */
    public function createMember()
    {
        $member_server = new memberServer();
        $member = $member_server->formatSignData();
        //验证接收值的正确与否
        $res = $this->validate($member,'Member');
        if(true !== $res){
            dingSendErrorMsgServer::sengMsg(commonServer::$lastShowMsg);
            //aw_log(commonServer::$lastShowMsg, 'member_res.log');
            commonServer::$lastShowMsg = $res;
            commonServer::error(2)->dies();
        }

        $field = $member_server->createUpdateMember($member);

        if(!$member_server->status){
            dingSendErrorMsgServer::sengMsg($member_server->error);
            aw_log([$member_server->error,$member], 'member_res.log');
            commonServer::error(10000,[], $member_server->error)->exits();
        }

        $ciphertext['id'] = $field['id'];
        $ciphertext['nick_name'] = $field['nick_name'];
        $data['key'] = $member_server->encryptionMemberInfo($ciphertext);

        $data['portrait'] = $field['portrait'];
        $data['status'] = $field['status'];
        $data['nick_name'] = $field['nick_name'];
        $data['is_new'] = $field['is_new'];
        commonServer::success($data, '登录成功')->exits();
    }

    /**
     * 确认订单
     */
    public function confirmOrder(){
        $store_id  = input('store_id', 0);
        $goods_list = input('goods_list', '');
        $key = input('key', '');

        //验证参数
        if(empty($goods_list) || empty($key)){
            commonServer::$showMsg = '参数不能为空';
            commonServer::error(2)->dies();
        }

        $goods_arr = json_decode($goods_list, true);
        if(!is_array($goods_arr)){
            $goods_arr = json_decode(htmlspecialchars_decode($goods_list), true);
        }

        if(empty($goods_arr)){
            commonServer::$showMsg = 'goods_list不能为空';
            commonServer::error(2)->dies();
        }

        //验证密钥是否合法
        $member_server = new memberServer();
        $member_info = $member_server->checkCiphertext($key);

        if($member_info === false){
            commonServer::error(2)->dies();
        }

        $old_member_info = $member_server->getOldMemberByNewId($member_info['id']);

        $order_server = new OrderServer();
        //todo aw:这个地方需要就用户id
        $data['m_id'] = $old_member_info['member_id'];
        $data['store_id'] = $store_id;
        $data['goods_list'] = $goods_list;

        $data_par = $order_server->confirmOrder($data);


        $token = md5($member_info['id'] . time() );
        $redis = getRedis( 1);
        $redis->set( 'order_token_' .$member_info['id']  ,  $token );
        $redis->pExpire( 'order_token_' .$member_info['id'] , 1000 * 7200 );
       // $token = request()->token('__token__');
        $data_par['token'] = $token;

        if($data_par === false){
            commonServer::error(20006,[], $order_server::$error)->dies();
        }
        commonServer::success($data_par,'确认订单信息获取成功')->dies();

    }

    /**
     * 新品弹框
     * @author 李高鹏^life <453885726@qq.com>
     */
    public function activity(){

        //弹窗开关
        $new_activity = config( 'site.new_activity');
        if( $new_activity == 0){
            commonServer::success([],'暂无活动')->dies();
        }
        $siteConf = config( 'site');
        //判断活动开启
        if($siteConf['new_activity'] != 1 ){
            commonServer::success([],'暂无活动')->dies();
        }

        $am_time = explode( '-' , $siteConf['am_time'] );
        $pm_time = explode( '-' , $siteConf['pm_time'] );
        $am_pics =  $siteConf['am_pics'];  //上午图片信息
        $pm_pics =  $siteConf['pm_pics'];  //下午图片信息

        $img = [];
        //判断当前是上午 还是下午
        $times = date('H:i' , time() );
        $date  = date('Y-m-d' , time() );
        if( $times >= $am_time[0]  &&   $times < $am_time[1]  ){
            //判断是否当天有活动
            if( isset($am_pics[$date]) &&  $am_pics[$date]){
                $img = explode( '||' , $am_pics[$date] );
            }
        }
        if( $times >= $pm_time[0]  &&   $times < $pm_time[1]  ){
            //判断是否当天有活动
            if( isset($pm_pics[$date]) &&  $pm_pics[$date]){
                $img = explode( '||' , $pm_pics[$date] );
            }
        }

        if( !$img ){
            $img = explode( '||' , $siteConf['default_pic'] );
        }

        commonServer::success($img )->dies();
    }

    /**
     * 获取小程序登录用户信息
     * code，通过code获取用户信息
     * @author  HQ.
     * email: huangqing@jpgk.com.cn
     * QQ:2322523770
     */
    public function getMiniWxUserLoginInfo(){

        $wxCode = input('get.wxCode');
        if(!$wxCode){
            return false;
        }

        $InfoData = (new Sns())->getSessionKey($wxCode);
        commonServer::success(json_decode($InfoData,true))->dies();
    }


}